<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['quotation:manager:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="managerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="日期" align="center" prop="date" width="100" />
      <el-table-column label="业务员" align="center" prop="name" />
      <el-table-column label="客户名称" align="center" prop="clientName" :show-overflow-tooltip="true" />
      <el-table-column label="产品名称" align="center" prop="productsName" :show-overflow-tooltip="true" />
      <el-table-column label="产品规格" align="center" prop="productsSpecification" :show-overflow-tooltip="true" />
      <el-table-column label="含税单价" align="center" prop="hsPrice" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="statusOptions" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Check"
            @click="handleAudit(scope.row)"
            v-hasPermi="['quotation:manager:audit']"
            v-if="scope.row.status === '总经理审核中'"
          >审批通过</el-button>
          <el-button
            link
            type="danger"
            icon="Close"
            @click="handleReject(scope.row)"
            v-hasPermi="['quotation:manager:reject']"
            v-if="scope.row.status === '总经理审核中'"
          >驳回</el-button>
          <el-button
            link
            type="primary"
            icon="View"
            @click="handleDetail(scope.row)"
            v-hasPermi="['quotation:manager:query']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情对话框 -->
    <el-dialog title="报价详情" v-model="open" width="1200px" append-to-body>
      <el-tabs type="border-card">
        <el-tab-pane label="报价单信息">
          <el-descriptions :column="3" border>
            <el-descriptions-item label="订单编号">{{ quoteInfo.quoQuote?.orderNo }}</el-descriptions-item>
            <el-descriptions-item label="日期">{{ quoteInfo.quoQuote?.date }}</el-descriptions-item>
            <el-descriptions-item label="业务员">{{ quoteInfo.quoQuote?.name }}</el-descriptions-item>
            <el-descriptions-item label="客户名称">{{ quoteInfo.quoQuote?.clientName }}</el-descriptions-item>
            <el-descriptions-item label="是否新客户">{{ quoteInfo.quoQuote?.newClient ? '是' : '否' }}</el-descriptions-item>
            <el-descriptions-item label="需要工程审核">{{ quoteInfo.quoQuote?.needEngineering ? '是' : '否' }}</el-descriptions-item>
            <el-descriptions-item label="产品名称">{{ quoteInfo.quoQuote?.productsName }}</el-descriptions-item>
            <el-descriptions-item label="产品规格">{{ quoteInfo.quoQuote?.productsSpecification }}</el-descriptions-item>
            <el-descriptions-item label="材料">{{ quoteInfo.quoQuote?.material }}</el-descriptions-item>
            <el-descriptions-item label="硬度">{{ quoteInfo.quoQuote?.hardness }}</el-descriptions-item>
            <el-descriptions-item label="产品颜色">{{ quoteInfo.quoQuote?.productsColor }}</el-descriptions-item>
            <el-descriptions-item label="公差">{{ quoteInfo.quoQuote?.tolerance }}</el-descriptions-item>
            <el-descriptions-item label="模具">{{ quoteInfo.quoQuote?.mold }}</el-descriptions-item>
            <el-descriptions-item label="物料号">{{ quoteInfo.quoQuote?.materialsNo }}</el-descriptions-item>
            <el-descriptions-item label="有图纸">{{ quoteInfo.quoQuote?.drawing ? '是' : '否' }}</el-descriptions-item>
            <el-descriptions-item label="用量">{{ quoteInfo.quoQuote?.dosage }}</el-descriptions-item>
            <el-descriptions-item label="工艺">{{ quoteInfo.quoQuote?.craft }}</el-descriptions-item>
            <el-descriptions-item label="线圈">{{ quoteInfo.quoQuote?.coils }}</el-descriptions-item>
            <el-descriptions-item label="切口">{{ quoteInfo.quoQuote?.incise }}</el-descriptions-item>
            <el-descriptions-item label="包装">{{ quoteInfo.quoQuote?.wrap }}</el-descriptions-item>
            <el-descriptions-item label="交期">{{ quoteInfo.quoQuote?.delivery }}</el-descriptions-item>
            <el-descriptions-item label="结算方式">{{ quoteInfo.quoQuote?.settlement }}</el-descriptions-item>
            <el-descriptions-item label="有照片">{{ quoteInfo.quoQuote?.photo ? '是' : '否' }}</el-descriptions-item>
            <el-descriptions-item label="有样品">{{ quoteInfo.quoQuote?.sample ? '是' : '否' }}</el-descriptions-item>
            <el-descriptions-item label="报价">{{ quoteInfo.quoQuote?.quote }}</el-descriptions-item>
            <el-descriptions-item label="含税单价">{{ quoteInfo.quoQuote?.hsPrice }}</el-descriptions-item>
            <el-descriptions-item label="状态">{{ quoteInfo.quoQuote?.status }}</el-descriptions-item>
          </el-descriptions>
        </el-tab-pane>
        <el-tab-pane label="工程审核信息" v-if="quoteInfo.quoEngineering">
          <el-descriptions :column="2" border>
            <el-descriptions-item label="模具">{{ quoteInfo.quoEngineering?.mold }}</el-descriptions-item>
            <el-descriptions-item label="规格">{{ quoteInfo.quoEngineering?.specification }}</el-descriptions-item>
            <el-descriptions-item label="材料">{{ quoteInfo.quoEngineering?.material }}</el-descriptions-item>
            <el-descriptions-item label="公差">{{ quoteInfo.quoEngineering?.tolerance }}</el-descriptions-item>
            <el-descriptions-item label="加工难度">{{ quoteInfo.quoEngineering?.difficulty }}</el-descriptions-item>
            <el-descriptions-item label="工程评价">{{ quoteInfo.quoEngineering?.appraise }}</el-descriptions-item>
            <el-descriptions-item label="备注" :span="2">{{ quoteInfo.quoEngineering?.remark }}</el-descriptions-item>
          </el-descriptions>
        </el-tab-pane>
        <el-tab-pane label="成本核算信息" v-if="quoteInfo.quoPricing">
          <el-descriptions :column="3" border>
            <el-descriptions-item label="材料费">{{ quoteInfo.quoPricing?.material }}</el-descriptions-item>
            <el-descriptions-item label="挤压费">{{ quoteInfo.quoPricing?.extrusion }}</el-descriptions-item>
            <el-descriptions-item label="后加工费">{{ quoteInfo.quoPricing?.backend }}</el-descriptions-item>
            <el-descriptions-item label="外协费">{{ quoteInfo.quoPricing?.outsourcing }}</el-descriptions-item>
            <el-descriptions-item label="组装费">{{ quoteInfo.quoPricing?.assemble }}</el-descriptions-item>
            <el-descriptions-item label="检验费">{{ quoteInfo.quoPricing?.inspection }}</el-descriptions-item>
            <el-descriptions-item label="包装费">{{ quoteInfo.quoPricing?.wrap }}</el-descriptions-item>
            <el-descriptions-item label="运输费">{{ quoteInfo.quoPricing?.transport }}</el-descriptions-item>
            <el-descriptions-item label="销售费">{{ quoteInfo.quoPricing?.sale }}</el-descriptions-item>
            <el-descriptions-item label="管理费">{{ quoteInfo.quoPricing?.manage }}</el-descriptions-item>
            <el-descriptions-item label="广告费">{{ quoteInfo.quoPricing?.ad }}</el-descriptions-item>
            <el-descriptions-item label="利润率">
              {{ quoteInfo.quoPricing?.profitmargin ? (quoteInfo.quoPricing.profitmargin * 100).toFixed(0) + '%' : '' }}
            </el-descriptions-item>
            <el-descriptions-item label="税率">
              {{ quoteInfo.quoPricing?.rate ? (quoteInfo.quoPricing.rate * 100).toFixed(0) + '%' : '' }}
            </el-descriptions-item>
            <el-descriptions-item label="未税单价">{{ quoteInfo.quoPricing?.wsPrice }}</el-descriptions-item>
            <el-descriptions-item label="含税单价">{{ quoteInfo.quoPricing?.hsPrice }}</el-descriptions-item>
            <el-descriptions-item label="备注" :span="3">{{ quoteInfo.quoPricing?.remark }}</el-descriptions-item>
          </el-descriptions>
        </el-tab-pane>
      </el-tabs>
      <template #footer v-if="actionType !== 'view'">
        <div class="dialog-footer">
          <el-button v-if="actionType === 'audit'" type="primary" @click="submitAudit">确认通过</el-button>
          <el-button v-if="actionType === 'reject'" type="danger" @click="submitReject">确认驳回</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, toRefs, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus'
import { listManagerQuote, getQuoteFullInfo, auditQuote, rejectQuote, exportManagerQuote } from "@/api/quotation/manager";

const { proxy } = getCurrentInstance();

// 遮罩层
const loading = ref(false);
// 选中数组
const ids = ref([]);
// 非单个禁用
const single = ref(true);
// 非多个禁用
const multiple = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 总经理审核表格数据
const managerList = ref([]);
// 是否显示弹出层
const open = ref(false);
// 当前操作类型: view/audit/reject
const actionType = ref('');
// 当前操作的记录
const currentRow = ref({});
// 报价单完整信息
const quoteInfo = ref({
  quoQuote: {},
  quoEngineering: null,
  quoPricing: null
});

// 状态数据字典
const statusOptions = ref([
  { label: '工程审核中', value: '工程审核中' },
  { label: '财务审核中', value: '财务审核中' },
  { label: '总经理审核中', value: '总经理审核中' },
  { label: '报价审核完成', value: '报价审核完成' },
  { label: '财务反审核完成', value: '财务反审核完成' }
]);

// 查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  orderNo: undefined,
  clientName: undefined,
  status: '总经理审核中'
});

/** 查询总经理审核列表 */
function getList() {
  loading.value = true;
  listManagerQuote(queryParams.value).then(response => {
    managerList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  actionType.value = '';
  quoteInfo.value = {
    quoQuote: {},
    quoEngineering: null,
    quoPricing: null
  };
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryForm");
  // 保持状态筛选为总经理审核中
  queryParams.value.status = '总经理审核中';
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 获取报价单完整信息 */
function getQuoteDetail(id) {
  getQuoteFullInfo(id).then(response => {
    quoteInfo.value = response.data;
    open.value = true;
  });
}

/** 审批通过按钮操作 */
function handleAudit(row) {
  currentRow.value = row;
  actionType.value = 'audit';
  getQuoteDetail(row.id);
}

/** 驳回按钮操作 */
function handleReject(row) {
  currentRow.value = row;
  actionType.value = 'reject';
  getQuoteDetail(row.id);
}

/** 详情按钮操作 */
function handleDetail(row) {
  currentRow.value = row;
  actionType.value = 'view';
  getQuoteDetail(row.id);
}

/** 提交审批通过 */
function submitAudit() {
  ElMessageBox.confirm(
    '确认审批通过该报价单吗？',
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    auditQuote({
      orderNo: quoteInfo.value.quoQuote.orderNo,
      id: quoteInfo.value.quoQuote.id
    }).then(response => {
      proxy.$modal.msgSuccess("审批通过成功");
      open.value = false;
      getList();
    });
  }).catch(() => {});
}

/** 提交驳回 */
function submitReject() {
  ElMessageBox.confirm(
    '确认驳回该报价单吗？',
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    rejectQuote({
      orderNo: quoteInfo.value.quoQuote.orderNo,
      id: quoteInfo.value.quoQuote.id
    }).then(response => {
      proxy.$modal.msgSuccess("驳回成功");
      open.value = false;
      getList();
    });
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('/quotation/manager/export', {
    ...queryParams.value
  }, `manager_${new Date().getTime()}.xlsx`);
}

onMounted(() => {
  getList();
});
</script> 